<template>
	<div class="wrapper">
		<div class="back-icon" @click="goBack()">
			<i class="fa fa-arrow-left"></i>
		</div>
		<!-- header部分 -->
		<header>
			<p>确认订单</p>
		</header>

		<!-- 订单信息部分 -->
		<div class="order-info">
			<h5>订单配送至</h5>
			<div class="order-info-address" @click="toUserAddress">
				<p>{{deliveryaddress!=null?deliveryaddress.address:'请选择送货地址'}}</p>
				<i class="fa fa-angle-right"></i>
			</div>
			<p v-if="deliveryaddress==null"></p>
			<p v-if="deliveryaddress!=null">{{deliveryaddress.contactName}}{{deliveryaddress.contactSex == 1?"先生":"女士"}} {{deliveryaddress.contactTel}}</p>
		</div>

		<h3>{{business.businessName}}</h3>

		<!-- 订单明细部分 -->
		<ul class="order-detailed">
			<li v-for="item in cartArr">
				<div class="order-detailed-left">
					<img :src="item.food.foodImg">
					<p>{{item.food.foodName}} x {{item.quantity}}</p>
				</div>
				<p>&#165;{{item.food.foodPrice*item.quantity}}</p>
			</li>
		</ul>
		<div class="order-deliveryfee">
			<p>配送费</p>
			<p>&#165;{{business.deliveryPrice}}</p>
		</div>
		<div class="order-credit">
			<p>积分</p>
			<p>可获得 {{parseInt(totalPrice())}} 个</p>
		</div>
		<div class="order-credit-discount">
			<div class="header" @click="detailetShow()">
				<div class="header-left">
					<p>使用积分抵扣</p>
					<i class="fa fa-caret-down"></i>
				</div>
				<p>{{discount}} 元</p>
			</div>
			<ul class="options" v-show="isShowDetailet">
				<li class="zero" @click="setDiscount('0')"><p>不使用积分抵扣</p><p>积分余额{{user.userCredit}}</p></li>
				<li class="one" @click="setDiscount('1')"><p>使用积分抵扣1元</p><p>积分-10</p></li>
				<li class="two" @click="setDiscount('2')"><p>使用积分抵扣2元</p><p>积分-20</p></li>
				<li class="five" @click="setDiscount('5')"><p>使用积分抵扣5元</p><p>积分-50</p></li>
			</ul>
		</div>

		<!-- 合计部分 -->
		<div class="total">
			<div class="total-left">
				&#165;{{totalPrice()}}
			</div>
			<div class="total-right" @click="toPayment">
				去支付
			</div>
		</div>
	</div>
</template>

<script>
	import {useRouter} from 'vue-router'
	import {useRoute} from 'vue-router'
	import {ref,onMounted,getCurrentInstance, reactive} from 'vue';
	
	export default{
		name:'Orders',
		setup(){
			const currentInstance = getCurrentInstance();
			const router = useRouter();
			const route = useRoute();
			const businessId = route.params.businessId;
			const business = ref({});
			const user = ref({});
			const cartArr = ref([]);
			const deliveryaddress = ref({});
			const discount = ref('0');
			const isShowDetailet = ref(false);
			const goBack = () => {
				router.go(-1);
			};
			const detailetShow = () => {
				isShowDetailet.value = !isShowDetailet.value;
			};
			const setDiscount = (Discount) => {
				if((Discount*10)>user.value.userCredit){
					swal("积分不足！");
					return;
				}
				discount.value = Discount;
			}
			const totalPrice = () => {
				let totalPrice = 0;
				for(let cartItem of cartArr.value){
					totalPrice += cartItem.food.foodPrice*cartItem.quantity;
				}
				totalPrice += business.value.deliveryPrice;
				totalPrice -= discount.value;
				return totalPrice;
			}
			
			const toUserAddress = () => {
				router.push({
					name:'UserAddress',
					params:{businessId:businessId},
				});
			}
			
			const toPayment = () => {
				if(deliveryaddress.value==null){
					swal('请选择送货地址！');
					return;
				}
				
				//创建订单
				currentInstance.appContext.config.globalProperties.$axios.post(
						'OrdersController/createOrders', currentInstance.appContext.config
						.globalProperties.$qs.stringify({
							userId:user.value.userId,
							businessId:businessId,
							daId:deliveryaddress.value.daId,
							orderTotal:totalPrice(),
							discount:discount.value,
							orderTime:Math.floor(new Date().getTime() / 1000)
						}))
					.then(response => {
						let orderId = response.data;
						if(orderId>0){
							router.push({name:'Payment',params:{orderId:orderId}})
						}else{
							swal('创建订单失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
				currentInstance.appContext.config.globalProperties.$axios.post(
						'UserController/UpdateCredit', currentInstance.appContext.config
						.globalProperties.$qs.stringify({
							userId:user.value.userId,
							credit:user.value.userCredit-discount.value*10
						}))
					.then(response => {
						if(response.data<1){
							swal("使用积分失败！");
						}
					})
					.catch(error => {
						console.error(error);
					});
				user.value.userCredit -= discount.value*10;
				currentInstance.appContext.config.globalProperties.$setSessionStorage('user',user.value);
			}
			
			onMounted(() => {
				user.value = currentInstance.appContext.config.globalProperties.$getSessionStorage('user');
				deliveryaddress.value = currentInstance.appContext.config.globalProperties.$getLocalStorage(user.value.userId);
				//查询当前商家
				currentInstance.appContext.config.globalProperties.$axios.post(
						'BusinessController/getBusinessById', currentInstance.appContext.config
						.globalProperties.$qs.stringify({
							businessId: businessId
						}))
					.then(response => {
						business.value = response.data;
					})
					.catch(error => {
						console.error(error);
					});
				//查询当前用户在购物车中的当前商家食品列表
				currentInstance.appContext.config.globalProperties.$axios.post(
						'CartController/listCart', currentInstance.appContext.config
						.globalProperties.$qs.stringify({
							userId:user.value.userId,
							businessId:businessId
						}))
					.then(response => {
						cartArr.value = response.data;
					})
					.catch(error => {
						console.error(error);
					});
			});
			
			return{
				businessId,business,user,cartArr,deliveryaddress,discount,isShowDetailet,setDiscount,
				totalPrice,toUserAddress,toPayment,goBack,detailetShow
			}
		}
	}
</script>

<style scoped>
	/*********** 总容器 ***********/
	.wrapper {
		width: 100%;
		height: 100%;
	}
	/*********** 回退按钮 ***********/
	.wrapper .back-icon{
		width: 12vw;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 1001;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	/*********** header部分 ***********/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;
		/* 这行代码保证header位于最顶层，不加这一行会导致商家图片遮挡header，因为默认情况下，绝对位置的元素后写的位于上层 */

		display: flex;
		justify-content: center;
		align-items: center;
	}

	/*********** 订单信息部分 ***********/
	.wrapper .order-info {
		width: 100%;
		margin-top: 12vw;
		background-color: #0097EF;
		box-sizing: border-box;
		padding: 2vw;
		color: #FFFFFF;
	}

	.wrapper .order-info h5 {
		font-size: 3vw;
		font-weight: 300;
	}

	.wrapper .order-info .order-info-address {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;

		font-weight: 700;
		user-select: none;
		cursor: pointer;
		margin: 1vw 0;
	}

	.wrapper .order-info .order-info-address p {
		width: 90%;
		font-size: 5vw;
	}

	.wrapper .order-info .order-info-address i {
		font-size: 6vw;
	}

	.wrapper .order-info p {
		font-size: 3vw;
	}

	.wrapper h3 {
		box-sizing: border-box;
		padding: 3vw;
		font-size: 4vw;
		color: #666;
		border-bottom: solid 1px #DDD;
	}

	/*********** 订单明细部分 ***********/
	.wrapper .order-detailed {
		width: 100%;
	}

	.wrapper .order-detailed li {
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666;

		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .order-detailed li .order-detailed-left {
		display: flex;
		align-items: center;
	}

	.wrapper .order-detailed li .order-detailed-left img {
		width: 10vw;
		height: 10vw;
	}

	.wrapper .order-detailed li .order-detailed-left p {
		font-size: 3.5vw;
		margin-left: 3vw;
	}

	.wrapper .order-detailed li p {
		font-size: 3.5vw;
	}

	.wrapper .order-deliveryfee {
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 3.5vw;
	}
	.wrapper .order-credit {
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 3.5vw;
	}
	.wrapper .order-credit-discount {
		width: 100%;
		height: 8vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666;
		align-items: center;
		font-size: 3.5vw;
	}
	.wrapper .order-credit-discount .header{
		
		display: flex;
		justify-content: space-between;
		align-items: center;
		user-select: none;
		cursor: pointer;
	}
	.wrapper .order-credit-discount .header .header-left{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.wrapper .order-credit-discount .header .header-left i{
		margin: 0 2vw;
	}
	.wrapper .order-credit-discount .options{
		margin: 3vw 0;
		align-items: center;
	}
	.wrapper .order-credit-discount .options li{
		margin: 3vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
		user-select: none;
		cursor: pointer;
	}

	/*********** 订单合计部分 ***********/
	.wrapper .total {
		width: 100%;
		height: 14vw;

		position: fixed;
		left: 0;
		bottom: 0;

		display: flex;
	}

	.wrapper .total .total-left {
		flex: 2;
		background-color: #505051;
		color: #fff;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .total .total-right {
		flex: 1;
		background-color: #38CA73;
		color: #fff;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;
		cursor: pointer;

		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>